package x.zero.service.sys;


import cn.hutool.core.util.ObjectUtil;
import org.beetl.sql.core.page.DefaultPageRequest;
import org.beetl.sql.core.page.PageRequest;
import org.beetl.sql.core.page.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import x.zero.bo.ColumnInfo;
import x.zero.bo.TableInfo;
import x.zero.mapper.GeneratorMapper;
import x.zero.pojo.entity.GenConfig;
import x.zero.util.GenUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service
public class GeneratorService {
    @Autowired
    GeneratorMapper generatorMapper;


    @SuppressWarnings("all")
    public Object getTables(String name, Integer pageNum,Integer pageSize) {

        PageRequest pageRequest = DefaultPageRequest.of(pageNum + 1,pageSize);
        PageResult pageResult = generatorMapper.pageTable(name, pageRequest);
//        List<TableInfo> tableInfos = new ArrayList<>();
//        for (Map<String, Object> obj : list) {
//
//            tableInfos.add(new TableInfo(obj.get("tableName"), obj.get("createTime"), obj.get("engine"), obj.get("coding"), ObjectUtil.isNotEmpty(obj.get("table_comment")) ? obj.get("table_comment").toString() : "-"));
//        }
//        Object totalElements = generatorMapper.count();
        return pageResult;
    }

    @SuppressWarnings("all")
    public Object getColumns(String name) {

        /*List<Map<String, String>> result = generatorMapper.listColumns(name);
        List<ColumnInfo> columnInfos = new ArrayList<>();
        for (Map<String, String> obj : result) {

            columnInfos.add(new ColumnInfo(obj.get("COLUMN_NAME"), obj.get("IS_NULLABLE"), obj.get("DATA_TYPE"), obj.get("COLUMN_COMMENT"), obj.get("COLUMNKEY"), obj.get("EXTRA"), null, "true"));
        }
        return PageUtil.toPage(columnInfos, columnInfos.size());*/
        List<Map<String, String>> result = generatorMapper.listColumns(name);
        return result;
    }

    public void generator(List<ColumnInfo> columnInfos, GenConfig genConfig, String tableName) {

        try {
            GenUtil.generatorCode(columnInfos, genConfig, tableName);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
